package com.keda.dao;

import com.keda.model.Notice;
import com.keda.util.DBUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class NoticeDao {
    public List<Notice> queryNotices() {

        Connection connection = DBUtil.createConnection();

        String sql = "SELECT id, title FROM t_notice ORDER BY createTime desc LIMIT 7";

        QueryRunner queryRunner = new QueryRunner();
        List<Notice> notices = null;
        try {
            notices = queryRunner.query(connection, sql,
                    new BeanListHandler<Notice>(Notice.class));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return notices;

    }
}
